package love.bot.forli.group.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import love.bot.forli.group.review.bean.Dto.QuestionAllDto;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.bean.Vo.*;
import love.bot.forli.group.review.entity.QuestionGroup;
import love.bot.forli.group.review.service.QuestionGroupService;
import love.bot.forli.group.review.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题控制器
 *
 * @author Kontori
 */
@Api(tags = "问题相关接口")
@RestController
@RequestMapping("/question")
@Transactional(rollbackFor = {Throwable.class})
public class QuestionController {

    private QuestionService questionService;

    @Autowired
    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    private QuestionGroupService questionGroupService;

    @Autowired
    public void setQuestionGroupService(QuestionGroupService questionGroupService) {
        this.questionGroupService = questionGroupService;
    }

    @ApiOperation("获取题组类型ID对应题组")
    @GetMapping("/type/{questionTypeId}")
    public Result<List<QuestionGroup>> groupType(@PathVariable("questionTypeId") Integer questionTypeId) {
        return Result.success(questionGroupService.list(new QueryWrapper<QuestionGroup>()
                .eq("question_type_id", questionTypeId)));
    }

    @ApiOperation("获取题组ID对应题目")
    @GetMapping("/group/{groupId}")
    public Result<List<QuestionAllDto>> group(@PathVariable("groupId") Integer groupId) {
        return Result.success(questionService.getQuestionInfosByGroupId(groupId));
    }

    @ApiOperation("获取题目ID对应题目")
    @GetMapping("/id/{questionId}")
    public Result<QuestionAllDto> question(@PathVariable("questionId") Integer questionId) {
        return Result.success(questionService.getQuestionInfoById(questionId));
    }

    @ApiOperation("新增题目")
    @PostMapping("/add")
    public Result<Object> addQuestion(@RequestBody AddQuestionVo addQuestionVo) {
        return questionService.addQuestion(addQuestionVo);
    }

    @ApiOperation("修改题目")
    @PostMapping("/edit")
    public Result<Object> editQuestion(@RequestBody EditQuestionVo editQuestionVo) {
        return questionService.editQuestion(editQuestionVo);
    }

    @ApiOperation("删除题目")
    @DeleteMapping("/delete/{questionId}")
    public Result<Object> deleteQuestion(@PathVariable("questionId") Integer questionId) {
        return questionService.deleteQuestion(questionId);
    }

}
